<?php

class Ship_schedule_controller extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
        
        Validator::is_access($this->session->userdata('user_id'));
    }

    function arriving(){

        $title = _LB_SHIP_SCHEDULE_ARRIVING;
        $section = "ship_schedule_arriving";
        $cfer = new Cfer(array(
        $title => base_url('ship_schedule/arriving')));
        
        $this->data['page_title'] = $title;

        $filter = array();
        $filter['name'] = $this->input->post('name');
        $filter['type'] = 1;
        $filter['start_date'] = $this->input->post('start_date');
        $filter['end_date'] = $this->input->post('end_date');
        $filter['country'] = $this->input->post('country');
        $filter['port'] = $this->input->post('port');
        $filter['region'] = $this->input->post('region');
        $ship_schedule = ShipSchedule::getList($filter);

        $this->data['ship_schedule'] = $ship_schedule;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        $country = Country::getList(array('disabled'=>'0'));
        $port = Port::getList();
        $region = Region::getList();
        $filter2 = array();
        $filter2['parent_id'] = 0;
        $filter2['type'] = 1;
        Menu::getMenuList($array_menus, $filter2);
        $this->data['region'] = $region;
        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['port'] = $port;

        $this->load->view('ship_arriving_list', $this->data);
    }

    function arriving_delete($id = null) {
        $ship_schedule = new ShipSchedule();

        if ($id && !$ship_schedule->get($id)) {
                redirect(base_url('ship_schedule/arriving'));
        }
        $ship_schedule->delete();
        redirect(base_url('ship_schedule/arriving'));
    }
    
    function arriving_form($id = null) {
        $section = "ship_schedule";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_SHIP_SCHEDULE_ARRIVING => 'ship_schedule/arriving',
            _LB_EDIT_SHIP_SCHEDULE_ARRIVING => base_url('ship_schedule/arriving_form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_SHIP_SCHEDULE_ARRIVING => 'ship_schedule/arriving',
            _LB_ADD_SHIP_SCHEDULE_ARRIVING => base_url('ship_schedule/arriving_form/')));
        }
        
        $act = $this->input->get_post('act');
    
        $ship_schedule = new ShipSchedule();

        if ($id && !$ship_schedule->get($id)) {
                redirect(base_url('ship_schedule/arriving'));
        }
        
        if ($act == ACT_VALID_EDIT) {
            $ship_schedule->id_wp_country = $this->input->post('country');
            $ship_schedule->ship_name = $this->input->post('ship_name');
            $ship_schedule->grt = $this->input->post('grt');
            $ship_schedule->dwt = $this->input->post('dwt');
            $ship_schedule->date_time = $this->input->post('date_time');
            $ship_schedule->arriving_port = $this->input->post('arriving_port');
            $ship_schedule->leaving_port = $this->input->post('leaving_port');
            $ship_schedule->draf = $this->input->post('draf');
            $ship_schedule->loa = $this->input->post('loa');
            $ship_schedule->goods = $this->input->post('goods');
            $ship_schedule->advertiser = $this->input->post('advertiser');
            $ship_schedule->type = 1;
            $ship_schedule->id_wp_region = $this->input->post('region');
            if ($ship_schedule->validateInput()) {
                if ($id) {
                    $ship_schedule->modified_date = date('Y-m-d H:i:s');
                    $ship_schedule->update();
                } else {
                    $ship_schedule->created_date = date('Y-m-d H:i:s');
                    $ship_schedule->insert();
                }
                redirect(base_url('ship_schedule/arriving'));
            }
        }

        $country = Country::getList(array('disabled'=>'0'));
        $port = Port::getList();
        $port2 = Port::getList();
        
		$array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        $region = Region::getList();
        
        $title = _LB_SHIP_SCHEDULE_ARRIVING;
        $this->data['ship_schedule'] = $ship_schedule;
        $this->data['title'] = $title;
        $this->data['region'] = $region;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('ship_schedule/arriving');
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['port'] = $port;
        $this->data['port2'] = $port2;
        $this->data['cfer'] = $cfer;
        $this->load->view('ship_arriving_form', $this->data);
    }
    
    function leaving(){

        $title = _LB_SHIP_SCHEDULE_LEAVING;
        $section = "ship_schedule_leaving";
        $cfer = new Cfer(array(
        $title => base_url('ship_schedule/leaving')));
        
        $this->data['page_title'] = $title;

        $filter = array();
        $filter['name'] = $this->input->post('name');
        $filter['type'] = 2;
        $filter['start_date'] = $this->input->post('start_date');
        $filter['end_date'] = $this->input->post('end_date');
        $filter['country'] = $this->input->post('country');
        $filter['port'] = $this->input->post('port');
        $filter['region'] = $this->input->post('region');
        $ship_schedule = ShipSchedule::getList($filter);

        $this->data['ship_schedule'] = $ship_schedule;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        $country = Country::getList(array('disabled'=>'0'));
        $port = Port::getList();
        $region = Region::getList();
        $filter2 = array();
        $filter2['parent_id'] = 0;
        $filter2['type'] = 1;
        Menu::getMenuList($array_menus, $filter2);
        $this->data['region'] = $region;
        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['port'] = $port;
        $this->data['cfer'] = $cfer;
        $this->load->view('ship_leaving_list', $this->data);
    }

    function leaving_delete($id = null) {
        $ship_schedule = new ShipSchedule();

        if ($id && !$ship_schedule->get($id)) {
                redirect(base_url('ship_schedule/leaving'));
        }
        $ship_schedule->delete();
        redirect(base_url('ship_schedule/leaving'));
    }
    function leaving_form($id = null) {
        $section = "ship_schedule_leaving";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_SHIP_SCHEDULE_LEAVING => 'ship_schedule/leaving',
            _LB_EDIT_SHIP_SCHEDULE_LEAVING => base_url('ship_schedule/leaving_form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_SHIP_SCHEDULE_LEAVING => 'ship_schedule/leaving',
            _LB_ADD_SHIP_SCHEDULE_LEAVING => base_url('ship_schedule/leaving_form/')));
        }
        
        $act = $this->input->get_post('act');

        $ship_schedule = new ShipSchedule();

        if ($id && !$ship_schedule->get($id)) {
                redirect(base_url('ship_schedule/leaving'));
        }
        
        if ($act == ACT_VALID_EDIT) {
            $ship_schedule->id_wp_country = $this->input->post('country');
            $ship_schedule->ship_name = $this->input->post('ship_name');
            $ship_schedule->grt = $this->input->post('grt');
            $ship_schedule->dwt = $this->input->post('dwt');
            $ship_schedule->date_time = $this->input->post('date_time');
            $ship_schedule->arriving_port = $this->input->post('arriving_port');
            $ship_schedule->leaving_port = $this->input->post('leaving_port');
            $ship_schedule->type = 2;
            $ship_schedule->draf = $this->input->post('draf');
            $ship_schedule->loa = $this->input->post('loa');
            $ship_schedule->goods = $this->input->post('goods');
            $ship_schedule->advertiser = $this->input->post('advertiser');
            $ship_schedule->id_wp_region = $this->input->post('region');
            if ($ship_schedule->validateInput()) {
                if ($id) {
                    $ship_schedule->modified_date = date('Y-m-d H:i:s');
                    $ship_schedule->update();
                } else {
                    $ship_schedule->created_date = date('Y-m-d H:i:s');
                    $ship_schedule->insert();
                }
                redirect(base_url('ship_schedule/leaving'));
            }
        }

        $country = Country::getList(array('disabled'=>'0'));
        $port = Port::getList();
        $port2 = Port::getList();
        
		$array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        $region = Region::getList();
        
        $title = _LB_SHIP_SCHEDULE_LEAVING;
        
        $this->data['ship_schedule'] = $ship_schedule;
        $this->data['title'] = $title;
        $this->data['region'] = $region;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('ship_schedule/leaving');
        $this->data['section'] = $section;
        $this->data['country'] = $country;
        $this->data['port'] = $port;
        $this->data['port2'] = $port2;
        $this->data['cfer'] = $cfer;
        $this->load->view('ship_leaving_form', $this->data);
    }
}